<template>
  <div style="height:100%;">
    <app-header></app-header>
    <vue-better-scroll class="wrapper"
                      ref="scrollqq"
                      :scrollbar="scrollbarObj"
                      :pullDownRefresh="pullDownRefreshObj"
                      :pullUpLoad="pullUpLoadObj"
                      :startY='parseInt(startY)'
                      @pulling-down="onPullingDown"
                      @pulling-up="onPullingUp">
     
      <div class="myTrainclass">
        <div class="train_div">
          <h3><span></span>我的培训班</h3>
          <div class="seach">
            <input type="text" placeholder="请输入培训班名称" v-model="listQuery.keyWords" @keyup.enter="search">
            <span @click="search"></span>
          </div>
        </div>
        <div class="type">
          <ul class="typeList1">
            <li @click="show=true">{{status}}</li>
          </ul>
        </div>
        <div class="curse_box">
          <template v-if="activeIndex == 0">
            <ul class="curse_list">
              <li v-for="(item, index) in trainClassList" :key="index" class="curse_item">
                <img :src="item.trainClassImg" />
                <div class="item_right">
                  <h4>{{item.trainClassName}}</h4>
                  <p v-if="!item.isApply">报名时间:{{item.applyStartDate|dateFormat('YYYY-MM-DD')}}至{{item.applyEndDate|dateFormat('YYYY-MM-DD')}}</p>
                  <div class="hours" v-if="item.isApply"><span>应修学时：{{item.needStudyTime}}</span><span>已修学时:{{item.hasStudyTime}}</span></div>
                  <div class="result" v-if="item.isApply">
                    <div style="color:#2269ff;font-size:0.32rem;">考试成绩:{{item.score?item.score:'-'}}分</div>
                  </div>
                  <div class="btnBox">
                    <router-link :to="{name:'enterpriseApply',query:{id:item.id}}" class="baoBtn" v-if="item.canApply&&item.isEnterprise">我要报名</router-link>
                    <router-link :to="{name:'apply',query:{id:item.id}}" class="baoBtn" v-if="item.canApply&&!item.isEnterprise">我要报名</router-link>
                    <router-link :to="{name:'accessTrain',query:{id:item.id}}" class="assesBtn" v-if="item.status==2&&item.isAudit==2">进入培训</router-link>
                    <router-link :to="{name:'trainDetail',query:{id:item.id}}" class="detailBtn">查看详情</router-link>               
                    <router-link :to="{name:'examRecord',query:{id:item.id}}" class="recordBtn" v-if="item.status==2&&item.isAudit==2">查看记录</router-link>
                    <a class="recordBtn" v-if="item.isAudit==3" @click="checkReason(item)">查看原因</a>
                  </div>
                </div> 
              </li>
            </ul>
          </template>

          <template v-if="activeIndex == 1">
            <ul class="curse_list">
              <li v-for="(item, index) in curseList" :key="index" class="curse_item">
                <img :src="item.src" />
                <div class="item_right">
                  <h4>{{item.title}}</h4>
                  <div class="btnBox">
                    <router-link to="unitStudent" class="baoBtn">我要报名</router-link>
                    <router-link :to="{name:'trainDetail',query:{id:item.id}}" class="detailBtn">查看详情</router-link>
                  </div>
                </div>
              </li>
            </ul>
          </template>
        </div>
        
        <div v-show="show">       
          <popup-picker :show.sync="show" :data="checkLevelList" v-model="beCheckedLevel" show-name @on-change='changeStatus'></popup-picker>
        </div>
        <div v-transfer-dom class="answer">
          <popup v-model="showLogOut">
            <div class="msgDiv">
              <img src="../../assets/images/notice.png">
              <p>是否确定退出!</p>
              <span class="confirmBtn1" @click="showLogOut=false">取&nbsp;&nbsp;消</span><span class="confirmBtn" @click="logOut">确&nbsp;&nbsp;定</span>
            </div>
          </popup>
        </div>
      </div>
      <div v-show="showIndustry">
        <popup-picker :show.sync="showIndustry" :data="industryList" show-name v-model="workType" @on-hide="selectWorkType">
        </popup-picker>
      </div>
    </vue-better-scroll>
    <div class="footer">
      <ul class="footerList">
        <li @click="changeTab(index,item.footername)" :class="ind == index?'actived':''" v-for="(item,index) in footerList" :key="index">
          <div class="icoimg"><img :src='ind == index?item.activednameimg:item.nameimg'></div>
          <div class="iconame">{{item.footername}}</div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
  import { PopupPicker, Group, Popup } from 'vux'
  import VueBetterScroll from "vue2-better-scroll"
  import { getStudentTrainClassPageList } from "@/api/trainClass";
  import Header from "../../components/header";
  import { logout } from "@/api/login";
   import { GetStudentType, SaveStudentWorkType } from '@/api/UserManager';

  export default {
    components: {
      "app-header": Header,
      PopupPicker,
      Group,
      VueBetterScroll,
      Popup
    },
    data() {
      return {
        title1: 'vux此功能测试',
        checkLevelList: [[{value:'',name:"全部"},{value:'1',name:'未开始'},{value:'2',name:'进行中'},{value:'3',name:'已结束'}]], // 检查级别
        beCheckedLevel: [""], // 被选择的检查级别
        trainClassList: [],
        curseList: [
          { src: require('../../assets/images/corse_img1.png'), title: '2020年全省应急管理普法学习常规知识', time: '2020-3-10至2020-4-10' },
          { src: require('../../assets/images/corse_img4.png'), title: '2020年全省应急管理普法学习常规知识', repairedhours: '48学时', studyhours: '52学时', examresult: '90' },
          { src: require('../../assets/images/corse_img2.png'), title: '2020年全省应急管理普法学习常规知识', repairedhours: '48学时', studyhours: '52学时', examresult: '59' },
          { src: require('../../assets/images/corse_img3.png'), title: '2020年全省应急管理普法学习常规知识', repairedhours: '48学时', studyhours: '52学时', examresult: '90' },
          { src: require('../../assets/images/corse_img3.png'), title: '2020年全省应急管理普法学习常规知识', repairedhours: '48学时', studyhours: '52学时', examresult: '90' },
          { src: require('../../assets/images/corse_img3.png'), title: '2020年全省应急管理普法学习常规知识', repairedhours: '48学时', studyhours: '52学时', examresult: '90' },
        ],
        typeList: [
          { typeName: '审核状态' },
          { typeName: '完成状态' },
        ],
        footerList: [
          { footername: '首页', nameimg: require('../../assets/images/home.png'), activednameimg: require('../../assets/images/home2.png') },
          { footername: '我的培训班', nameimg: require('../../assets/images/mytrain.png'), activednameimg: require('../../assets/images/mytrain2.png') },
          { footername: '退出', nameimg: require('../../assets/images/outhui2x.png'), activednameimg: require('../../assets/images/outlan2x.png') }
        ],
        listQuery: {
          pi: 1,
          ps: 4,
          skipCount: 0,
          maxResultCount: 4,
          keyWords: '',
          searchType:'trainClassName',
          status:''
        },
        activeIndex: 0,
        ind: 1,
        scrollbarObj: { fade: true },
        pullDownRefreshObj: {
          threshold: 90,
          stop: 40
        },
        pullUpLoadObj: {
          threshold: 0,
          txt: {
            more: "加载更多",
            noMore: "没有更多数据了"
          }
        },
        startY: 0,
        scrollToX: 0,
        scrollToY: 0,
        scrollToTime: 700,
        showLogOut: false,
        status: "全部",
        show: false,
        showIndustry:false,
        industryList:[[{ name: '应急学员', value: '1' }]],
        industryId:'',
        workType:[]
      }
    },
    created() {
      GetStudentType().then(response=>{
        if(response.isSystemStudent){          
          var tempList=[];
          response.workerTypeList.forEach(item=>{
            tempList.push({name: item.labelName, value: item.id });
          });
          this.$set(this.industryList, 0, tempList);
          this.industryId=response.hyList[0].id;
          this.showIndustry=true;
        }
      });
      this.activeIndex = 0;
      this.search();
    },
    methods: {
      selectWorkType(val){
        if(val){
          let model={
                industryId: this.industryId,
                workType: this.workType[0]
              };
          SaveStudentWorkType(model).then(response => {
            
          })
        }else{
          this.$vux.alert.show({
            content: '请选择工种',
          });
          this.showIndustry=true;
        }
      },
      logOut() {
        this.$store.dispatch('FedLogOut').then(() => {
          location.reload() // 为了重新实例化vue-router对象 避免bug
        })
      },
      changeTab(index, name) {
        this.ind = index
        if (index == 0) {
          this.$router.push({
            path: '/studentIndex'
          })
        } else if (index == 2) {
          this.showLogOut = true;
        }
      },
      changeCheckLevel(item) {
        console.log(item)
        this.beCheckedLevel = item
        // this.onPullingDown()
      },
      search() {
        let that = this;
        that.listQuery.pi = 1;
        that.listQuery.skipCount = (that.listQuery.pi - 1) * that.listQuery.ps;
        this.$vux.loading.show({
          text: "Loading"
        });
        getStudentTrainClassPageList(this.listQuery).then(response => {
          this.trainClassList = response.items;
          this.$refs['scrollqq'].forceUpdate(true);
          this.$vux.loading.hide();
        })
      },
      changeStatus(val){
        this.show=false;
        this.listQuery.status=this.beCheckedLevel[0];
        this.status=this.checkLevelList[0].filter(x=>x.value==this.listQuery.status)[0].name;
        this.search();
      },
      fetchData() {
        let that = this;
        this.$vux.loading.show({
          text: "Loading"
        });
        getStudentTrainClassPageList(this.listQuery).then(response => {
          this.trainClassList = this.trainClassList.concat(response.items);
          let totalAllCount = response.totalCount;
          let resultCount = this.trainClassList.length;
          if (parseInt(resultCount) < parseInt(totalAllCount)) {
            this.$refs['scrollqq'].forceUpdate(true);
          } else {
            this.$refs['scrollqq'].forceUpdate(false);
          }
          this.$vux.loading.hide();
        })
      },
      onPullingUp() {
        let limitcount = this.trainClassList.length;
        this.listQuery.skipCount = limitcount;
        this.fetchData();
      },
      onPullingDown() {
        this.search();
      },
      checkReason(item){
        this.$vux.alert.show({
          title:'未通过原因:',
          content: item.reason,
          onShow(){
            document.getElementsByClassName('weui-dialog__hd')[1].style.color='red';
            document.getElementsByClassName('weui-dialog__bd')[1].style.color='red';
          }
        });
      }
    }
  }
</script>

<style scoped>
  .answer .vux-popup-dialog {
    position: fixed;
    left: calc((100% - 290px)/2);
    top: 150px;
    width: 290px;
    height: 170px !important;
    border-radius: 10px;
    background: #ffffff;
    overflow-y: visible;
  }

  .msgDiv {
    text-align: center;
    padding: 13px 25px 20px 25px;
  }

    .msgDiv img {
      height: 50px;
      width: 50px;
    }

    .msgDiv p {
      font-size: 20px;
      color: #fe6000;
      font-weight: bold;
      margin-top: 5px;
    }

  .confirmBtn {
    display: inline-block;
    height: 40px;
    font-size: 15px;
    background-color: #2269ff;
    font-weight: bold;
    line-height: 40px;
    width: 100px;
    color: #ffffff;
    text-align: center;
    border-radius: 10px;
    margin-top: 16px;
  }

  .confirmBtn1 {
    display: inline-block;
    height: 40px;
    font-size: 15px;
    font-weight: bold;
    line-height: 40px;
    width: 100px;
    color: #2269ff;
    text-align: center;
    border-radius: 10px;
    margin-top: 16px;
    border: solid 1px #2269ff;
    margin-right: 10px;
  }
  .myTrainclass {
    background: #ebeef2;
  }

  .header {
    height: 55px;
    background: #2269ff;
    color: #fff;
    padding: 0 15px;
    text-align: left;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .test {
    width: 100%;
    background: #fff;
    height: 47px;
    line-height: 47px;
  }

  .footer {
    width: 100%;
    height: 51px;
    background: #fff;
    position: fixed;
    bottom: 0;
    z-index: 999;
    border-top: 1px solid #ebeef2;
    box-sizing: border-box;
  }

  .footerList {
    width: 80%;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 50px;
  }

    .footerList li {
      width: 50%;
    }

      .footerList li.actived .iconame {
        color: #206aff;
      }

  .icoimg img {
    /* display: block; */
    width: 20px;
    height: 20px;
  }

  .icoimg, .iconame {
    font-size: 10px;
    color: #999;
  }

  .header h1 {
    font-size: 18px;
    font-weight: bold;
  }

  .train_div {
    background: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 50px;
    padding: 0 15px;
    margin-bottom: 5px;
    padding-top:70px;
  }

    .train_div h3 {
      font-size: 16px;
      color: #333;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

      .train_div h3 span {
        height: 16px;
        width: 4px;
        border-radius: 2px;
        display: block;
        background: #2269ff;
        margin-right: 5px;
      }

  .seach {
    width: 220px;
    height: 33px;
    background: #eee;
    border-radius: 10px;
    text-align: left;
    position: relative;
  }

    .seach input {
      appearance: none;
      background: transparent;
      border: none;
      height: 33px;
      width: calc(100% - 35px);
      outline: none;
      font-size: 14px;
      box-sizing: border-box;
      padding: 0 5px 0 10px;
    }

    .seach span {
      position: absolute;
      display: block;
      width: 30px;
      height: 33px;
      background: url('../../assets/images/search_icon.png') center center no-repeat;
      background-size: 15px 15px;
      right: 10px;
      top: 0;
    }

  .type {
    background: #fff;
    margin-bottom: 5px;
    height: 40px;
    display: flex;
    box-sizing: border-box;
    padding: 0 15px;
    align-items: center;
    position: relative;
    justify-content: space-between;
  }

  .typeList1 {
    width: 65%;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
  }

    .typeList1 li {
      font-size: 13px;
      font-weight: 700;
      position: relative;
    }

      .typeList1 li:after {
        content: '';
        position: absolute;
        top: 35%;
        left: 110%;
        border-width: 5px;
        border-style: solid;
        border-color: #bbb transparent transparent transparent;
      }

      .typeList1 li.active {
        color: #246aff;
      }

        .typeList1 li.active:after {
          border-color: #226cff transparent transparent transparent;
        }

  ul, ul li {
    list-style: none;
    font-size: 14px;
  }


  .curse_box {
    padding: 15px;
    background: #fff;
    padding-bottom: 45px;
  }

    .curse_box .hours {
      height: 10px;
      line-height: 10px;
      text-align: left;
      /* display: flex;
      justify-content: space-between; */
    }

    .curse_box .result {
      display: flex;
      justify-content: space-between;
      height: 26px;
      line-height: 26px;
      /* margin-top: 6px; */
    }

      .curse_box .result .green {
        color: #2269ff
      }

      .curse_box .result .red {
        color: red
      }

      .curse_box .result .examresult {
        font-size: 12px;
        color: #236aff;
      }

      .curse_box .result .trainBtn {
        width: 65px;
        height: 25px;
        line-height: 25px;
        background: #2269ff;
        color: #fff;
        text-align: center;
        font-size: 12px;
        border-radius: 5px;
      }

    .curse_box .hours span {
      font-size: 12px;
      color: #999;
    }

  .curse_list {
    padding-top: 10px;
  }

    .curse_list li:last-child {
      margin-bottom: 0;
    }

  .curse_item {
    display: flex;
    justify-content: space-between;
    margin-bottom: 25px;
    height: 90px;
  }

    .curse_item img {
      width: 135px;
      height: 90px;
    }

  .curse_list li:first-child .item_right {
    margin-top: 0;
  }

  .item_right {
    margin-left: 10px;
    width: calc(100% - 145px);
    margin-top: -5px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding:2px 0;
  }

    .item_right h4 {
      text-align: left;
      font-size: 15px;
      font-weight: bold;
      color: #333;
      white-space: nowrap;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-bottom: 3px;
    }

    .item_right p {
      background: url("../../assets/images/time_icon.png") left center no-repeat;
      background-size: 11px 11px;
      padding-left: 13px;
      color: #999;
      font-size: 12px;
      text-align: left;
    }

  .btnBox {
    text-align: left;
    /* margin-top: 10px; */
    height: 25px;
    display: flex;
    
  }
   .btnBox a:nth-child(1){
     margin-left:0;
   }
    .btnBox a {
      display: inline-block;
      width: 65px;
      height: 25px;
      text-decoration: none;
      text-align: center;
      line-height: 25px;
      font-size: 12px;
      border-radius: 5px;
      box-sizing: border-box;
      border: 1px solid #2269ff;
      color: #2269ff;
      margin-left:5px;
    }

    .btnBox .baoBtn {
      background: #2269ff;
      color: #fff;
    }
</style>
